layui.define([ 'form','layer','memberApi','laydate','laytpl' ], function(exports) {
    var view = {};
    var form = layui.form;
    var $ = layui.$;
    var laydate = layui.laydate;
    var laytpl = layui.laytpl;
    var memberApi = layui.memberApi;
    var topBgListViewId = "topBgListView";
    var topBgListViewTplId = "topBgListTpl";

    $(document).on("click",".topBg_del",function() {
        memberApi.topBgDel(function() {
            Common.info("删除头部背景成功");
        });
    })
    $(document).on("click",".bg_li .marker",function() {
        var bgId = $(this).attr("bgId")
        $(".bg_li .marker").removeClass("currentBg");
        $(this).addClass("currentBg");
        var imgSrc = $(this).parent().find("img").attr("src");
        $("#space_top").html("<img class='spaceTopImg' src='" + imgSrc + "' />");

        memberApi.topBgSave(bgId,function() {
            Common.info("更换头部背景成功");
        });
        //滚动到顶部
        $(document).scrollTop(0);
    })
    function initTopBgList(id, page,limit) {
        var $el = $("#" + id);
        var memberId = $el.attr("member_id");
        if (!page) page = 1;
        if (!limit) limit = 10;
        $.post(Common.ctxPath + "/space/topBg/list.json",{page:page,limit:limit},function(resp) {
           resp.limit = limit;
           console.log(resp);
           var topBgListTpl = document.getElementById(topBgListViewTplId);
           if (topBgListTpl) {
               var getTpl = topBgListTpl.innerHTML;
               laytpl(getTpl).render(resp, function(html){
                  topBgListView.innerHTML = html;
               });
           }
       });
    }

    function initTopBgListPage(count,curr,limit) {
        return laypage.render({
            elem: 'topBgPage'
            ,count: count
            ,curr:curr
            ,limit:limit
            ,layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
            ,jump: function(obj,first){
              //首次不执行
              if(!first){
                  initTopBgList(topBgListViewId, obj.curr,limit);
              }
            }
          });
    }

    initTopBgList(topBgListViewId,1,10);

	exports('topBg', view);
});